﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace BawangMemo.BawangMemoView.View
{
    /// <summary>
    /// Settings.xaml 的交互逻辑
    /// </summary>
    public partial class Bawang_Setting : Window
    {
        public static int _minute = 15;
        public static bool _enter_sound = true;
        //public static bool _sound = true;
        public static int skinIndex = -1;
        
        private static List<string> condition;

        static int[] MinArr = { 1, 2, 5, 10, 15, 30, 40, 60, 120, 240, 999999 };
        static string[] ShowArr = { "1 分钟", "2 分钟", "5 分钟", "10 分钟", "15 分钟", "30 分钟", "40 分钟", "1 小时", "2 小时", "4 小时", "  从不" };

        ResourceDictionary oldDict;
        Brush oldBrush;

        public Bawang_Setting()
        {
            InitializeComponent();
            //Init Now
            condition = funcs.getInputDocs2(@"settings.txt");
            this.slider1.Value = int.Parse(condition[0]);
            checkBox1.IsChecked = bool.Parse(condition[1]);
            //checkBox2.IsChecked = bool.Parse(condition[2]);


            int value = (int)this.slider1.Value;
            _minute = MinArr[value];
            this.label4.Content = "  " + ShowArr[value];
            _enter_sound = bool.Parse(condition[1]);
            //_sound = bool.Parse(condition[2]);
            skinIndex = Int32.Parse(condition[2]);

            // backup
            oldDict = Application.Current.Resources.MergedDictionaries[0];
            oldBrush = (Brush)FindResource("SkinColor");
        }

        public static void MyInit()
        {
            condition = funcs.getInputDocs2(@"settings.txt");
            int value = int.Parse(condition[0]);
            _minute = MinArr[value];
            
            _enter_sound = bool.Parse(condition[1]);
            //_sound = bool.Parse(condition[2]);
            
        }

        private void slider1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            //1分钟 2分钟 5分钟 10分钟 15分钟 30分钟 40分钟 1小时 2小时 4小时 从不
            int value = (int)this.slider1.Value;
            _minute = MinArr[value];

            this.label4.Content = "  " + ShowArr[value];
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //按确定才更改Settings文件，否则不更改，直接退出不更改。
            List<string> lines = new List<string>();
            lines.Add(((int)this.slider1.Value).ToString());
            lines.Add(_enter_sound.ToString());
            lines.Add(skinIndex.ToString());
            funcs.WriteLines(@"settings.txt", lines);
            this.Close();            
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Resources.MergedDictionaries[0] = oldDict;
            MainWindowManager._mainWindow.toolBar1.Background = oldBrush;
            this.Close();
        }


        private void checkBox1_Checked(object sender, RoutedEventArgs e)
        {
            _enter_sound = true;
         
        }

        //private void checkBox2_Checked(object sender, RoutedEventArgs e)
        //{
        //    _sound = true;            
        //}

        private void checkBox1_Unchecked(object sender, RoutedEventArgs e)
        {
            _enter_sound = false;
        }

        //private void checkBox2_Unchecked(object sender, RoutedEventArgs e)
        //{
        //    _sound = false;
        //}

        private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (skinIndex >= 0)
            {
                Application.Current.Resources.MergedDictionaries[0] = new ResourceDictionary()
                {
                    Source = new Uri("Skin/" + ((ComboBoxItem)comboBox1.SelectedItem).Content + ".xaml",
                      UriKind.Relative)
                };
                MainWindowManager._mainWindow.toolBar1.Background = (Brush)FindResource("SkinColor");
                skinIndex = comboBox1.SelectedIndex;
            }
        }

      
    }
}
